package com.neuedu.shixun.hospital2.service.Impl;
import com.neuedu.shixun.hospital2.entity.JiBing;
import com.neuedu.shixun.hospital2.entity.JiBing2;
import com.neuedu.shixun.hospital2.entity.Ks;
import com.neuedu.shixun.hospital2.entity.User;
import com.neuedu.shixun.hospital2.repository.JiBingRepository;
import com.neuedu.shixun.hospital2.repository.KsRepository;
import com.neuedu.shixun.hospital2.service.JiBingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Slf4j
@Service
public class JiBingServiceImpl implements JiBingService{
    @Autowired
    private JiBingRepository jiBingRepository;
    @Autowired
    private KsRepository ksRepository;
    @Override
    public List<JiBing> getJiBing() {
        return jiBingRepository.findAll();
    }

    @Override
    public List<JiBing> getAll() {
        List<JiBing> list=jiBingRepository.findAll();
        return list;
    }
    @Override
    public List<JiBing2> getAll2(List<JiBing> list) {
        List<JiBing2> list1=new ArrayList<>();
        for (JiBing jiBing:list){
            JiBing2 jiBing2=new JiBing2();
            jiBing2.setBbid(jiBing.getBid());
            jiBing2.setBbname(jiBing.getJbname());
            jiBing2.setKkid(jiBing.getKid());
            Ks ks=new Ks();
            try{
                Optional<Ks> optionalKs=ksRepository.findById(jiBing.getKid());
                if (optionalKs.isPresent()){
                    ks=optionalKs.get();
                }
            }catch (Exception e){

            }
            jiBing2.setKkname(ks.getKKname());
            list1.add(jiBing2);
            log.info(""+list1);
        }
         return list1;
    }

    @Override
    public JiBing2 update(JiBing2 jiBing2) {
        if (jiBing2.getKkname()!=null){//修改
         Ks ks=new Ks();
         ks.setKKid(jiBing2.getKkid());
          ks.setKKname(jiBing2.getKkname());
          ksRepository.save(ks);
        }
        JiBing jiBing=new JiBing();
        jiBing.setJbname(jiBing2.getBbname());
        if (jiBing2.getBbid()!=null){
          jiBing.setBid(jiBing2.getBbid());
        }
        jiBing.setKid(jiBing2.getKkid());
        jiBingRepository.save(jiBing);
        return null;
    }
//    @Override
//    public Page<JiBing2> findByPage(int pageCode, int pageSize) {
//        Pageable pageable = new PageRequest(pageCode, pageSize);
//        return jiBing2Repository.findAll(pageable);
//    }

    @Override
    public void delete(Integer bbid) {
         jiBingRepository.deleteById(bbid);
    }
    @Override
    public Page<JiBing2> findByPage(int pageCode, int pageSize) {
        Pageable pageable = new PageRequest(pageCode, pageSize);
        Page<JiBing> jiBingPage=jiBingRepository.findAll(pageable);
        log.info("jibingaaaaa"+jiBingPage);
        List<JiBing2> list=getAll2(jiBingPage.getContent());
        log.info("jibing2sssss"+list);
       return new PageImpl<>(list);
    }
    @Override
    public int getNum() {
        return getAll().size();
    }

    @Override
    public JiBing2 getOneById(int bbid) {
        JiBing jiBing=jiBingRepository.getOne(bbid);
         JiBing2 jiBing2=getOne2(jiBing);
         return jiBing2;
    }
    @Override
    public JiBing2 getOneByName(String bname) {
        JiBing jiBing=jiBingRepository.findByJbname(bname);
        JiBing2 jiBing2=getOne2(jiBing);
        return jiBing2;
    }

    @Override
    public List<JiBing2> searchByName(List<JiBing2> list,String name) {
        List<JiBing2> list1=new ArrayList<>();
        for (JiBing2 jiBing2:list){
            if (jiBing2.getBbname().indexOf(name)!=-1){
               JiBing2 jiBing21= getOneByName(jiBing2.getBbname());
               list1.add(jiBing21);
            }
        }
        return list1;
    }

    public JiBing2 getOne2(JiBing jiBing){
        JiBing2 jiBing2=new JiBing2();
        jiBing2.setKkid(jiBing.getKid());
        jiBing2.setBbname(jiBing.getJbname());
        jiBing2.setBbid(jiBing.getBid());
        Optional<Ks> optionalKs=ksRepository.findById(jiBing.getKid());
        Ks ks=new Ks();
        if(optionalKs.isPresent()){
           ks=optionalKs.get();
        }
        jiBing2.setKkname(ks.getKKname());
        return jiBing2;
    }

}
